package com.scsj.supermarket.view.activity.mainmodel;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.TreasureAdapter;
import com.scsj.supermarket.base.TreasureBase;
import com.scsj.supermarket.bean.TreasureBean;
import com.scsj.supermarket.presenter.TreasurePresenter;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;


/**
 * 百宝箱
 */
public class TreasureActivity extends BaseActivity implements TreasureBase.IView {

    private RecyclerView treasure_rv;
    private TreasurePresenter presenter;
    private TreasureAdapter adapter;
    private List<TreasureBean> treasureBeans;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_treasure_layout);

        initToolbar();
        setToolbarTitle("百宝箱");
        hasBack(true);
    }

    @Override
    protected void initView() {
        treasure_rv = findViewById(R.id.treasure_rv);
        treasure_rv.setLayoutManager(new GridLayoutManager(this, 5, LinearLayoutManager.HORIZONTAL, false) {
            @Override
            public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
                super.onMeasure(recycler, state, widthSpec, heightSpec);
                if (getChildCount() > 0) {
                    View firstChildView = recycler.getViewForPosition(0);
                    measureChild(firstChildView, widthSpec, heightSpec);
                    setMeasuredDimension(View.MeasureSpec.getSize(widthSpec), firstChildView.getMeasuredHeight() * 6);
                } else {
                    super.onMeasure(recycler, state, widthSpec, heightSpec);
                }
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

        treasureBeans = new ArrayList<>();

        List<TreasureBean.Treasure> data = new ArrayList<>();
        data.add(new TreasureBean.Treasure(R.mipmap.all, ""));
        data.add(new TreasureBean.Treasure(R.mipmap.all, ""));
        data.add(new TreasureBean.Treasure(R.mipmap.all, ""));
        data.add(new TreasureBean.Treasure(R.mipmap.all, ""));
        treasureBeans.add(new TreasureBean(0, data));
        treasureBeans.add(new TreasureBean(0, data));
        treasureBeans.add(new TreasureBean(0, data));
        treasureBeans.add(new TreasureBean(0, data));
        treasureBeans.add(new TreasureBean(0, data));
        adapter = new TreasureAdapter(treasureBeans, this);
        treasure_rv.setAdapter(adapter);

    }

    @Override
    public void showSuccessMsg(String s) {
        Toasty.success(this, s).show();

    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this, s).show();

    }

    @Override
    public void setData(TreasureBean treasureBean) {

    }
}
